﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MySql.Data.MySqlClient;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }




    protected void Button1_Click(object sender, EventArgs e)
    {
        String user = TextBox1.Text;
        String upw = TextBox2.Text;
        if (user == "")
        {
            Response.Write("<script>alert('账号不能为空')</script>");
        }
        else if (upw == "")
        {
            Response.Write("<script>alert('密码不能为空')</script>");
        }
        else
        {
            String connString = "server=localhost;database=test;uid=root;pwd=123456";
            MySqlConnection conn = new MySqlConnection(connString);
            try
            {
                conn.Open();
                String sql = "select count(*) from test.webuser where username= '" + user + "' and password = '" + upw + "'";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                
                if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
                {
                    Session.Remove("username");
                    Session["username"] = user;
                    String t = DateTime.Now.ToString();

                    String sql2 = "update test.webuser set time='" + t + "',count=count+1 where username = '" + user + "';";
                    MySqlCommand cmd2 = new MySqlCommand(sql2, conn);
                    cmd2.ExecuteNonQuery();
                    conn.Close();
                    Response.Redirect("http://localhost:50015/index.aspx", true);
                }
                else
                {
                    Response.Write("<script>alert('登录失败，请确认你的账户密码');location.href='http://localhost:50015/Login.aspx';</script>");
                }
               

                // Response.Redirect("http://localhost:50015/Login.aspx", true);


            }
            catch
            {
                conn.Close();
                Response.Write("<script>alert('注册失败，联系管理员修复')</script>");
            }

        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://localhost:50015/Register.aspx", true);
    }

   
}